package WatchManager

import(
	"github.com/fsnotify/fsnotify"
)


type WatchManager struct {
	watcher *fsnotify.Watcher
	watched map[string]bool // 跟踪已监听的路径
}

func NewWatchManager() (*WatchManager, error) {
	w, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}
	return &WatchManager{
		watcher: w,
		watched: make(map[string]bool),
	}, nil
}

func (wm *WatchManager) Add(path string) error {
	if wm.watched[path] {
		return nil // 已经监听
	}
	if err := wm.watcher.Add(path); err != nil {
		return err
	}
	wm.watched[path] = true
	return nil
}

func (wm *WatchManager) Remove(path string) error {
	if !wm.watched[path] {
		return nil // 未监听
	}
	if err := wm.watcher.Remove(path); err != nil {
		return err
	}
	delete(wm.watched, path)
	return nil
}

func (wm *WatchManager) Close() {
	wm.watcher.Close()
}

/*
实际应用场景
动态配置更新：

go
// 当配置文件路径变更时
watcher.Remove(oldConfigPath)
watcher.Add(newConfigPath)
临时文件监控：

go
// 处理完临时文件后取消监听
defer watcher.Remove(tempFile)
目录结构变化：

go
// 当目录被删除时自动取消监听
if event.Op&fsnotify.Remove == fsnotify.Remove {
    watcher.Remove(event.Name)
}
*/
